package com.bank.ui.admin;

import com.bank.dao.CardDAO;
import com.bank.entity.Card;
import javax.swing.*;
import java.awt.*;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class EditCardDialog extends JDialog {
    private final CardDAO cardDAO = new CardDAO();
    private final String cardID;
    private JTextField balanceField;
    private JPasswordField passwordField;
    private JComboBox<String> isReportLossCombo;
    private JTextField openDateField; // 新增开户日期字段

    public EditCardDialog(String cardID) {
        this.cardID = cardID;
        initializeUI();
        setModal(true);
        pack();
        setLocationRelativeTo(null);
    }

    private void initializeUI() {
        setTitle("编辑银行卡信息");
        setLayout(new BorderLayout());
        setResizable(false);

        // 主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.WEST;

        try {
            Card card = cardDAO.getCardByID(cardID);
            if (card == null) {
                JOptionPane.showMessageDialog(this, "银行卡不存在！", "错误", JOptionPane.ERROR_MESSAGE);
                dispose();
                return;
            }

            // 卡号标签（不可编辑）
            gbc.gridx = 0;
            gbc.gridy = 0;
            mainPanel.add(new JLabel("卡号:"), gbc);

            JTextField cardIDField = new JTextField(card.getCardID(), 20);
            cardIDField.setEditable(false);
            gbc.gridx = 1;
            mainPanel.add(cardIDField, gbc);

            // 余额
            gbc.gridx = 0;
            gbc.gridy = 1;
            mainPanel.add(new JLabel("余额:"), gbc);

            balanceField = new JTextField(String.valueOf(card.getBalance()), 20);
            gbc.gridx = 1;
            mainPanel.add(balanceField, gbc);

            // 密码
            gbc.gridx = 0;
            gbc.gridy = 2;
            mainPanel.add(new JLabel("密码:"), gbc);

            passwordField = new JPasswordField(card.getPassword(), 20);
            gbc.gridx = 1;
            mainPanel.add(passwordField, gbc);

            // 是否挂失
            gbc.gridx = 0;
            gbc.gridy = 3;
            mainPanel.add(new JLabel("是否挂失:"), gbc);

            isReportLossCombo = new JComboBox<>(new String[]{"是", "否"});
            isReportLossCombo.setSelectedItem(card.getIsReportLoss());
            gbc.gridx = 1;
            mainPanel.add(isReportLossCombo, gbc);

            // 新增：开户日期字段
            gbc.gridx = 0;
            gbc.gridy = 4;
            mainPanel.add(new JLabel("开户日期:"), gbc);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            openDateField = new JTextField(sdf.format(card.getOpenDate()), 20);
            gbc.gridx = 1;
            mainPanel.add(openDateField, gbc);

        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "加载银行卡信息失败！", "错误", JOptionPane.ERROR_MESSAGE);
            dispose();
            return;
        }

        // 按钮面板
        JPanel buttonPanel = new JPanel();
        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(e -> saveChanges());

        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(saveButton);
        buttonPanel.add(cancelButton);

        // 添加面板到对话框
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private void saveChanges() {
        try {
            Card card = cardDAO.getCardByID(cardID);
            if (card == null) {
                JOptionPane.showMessageDialog(this, "银行卡不存在！", "错误", JOptionPane.ERROR_MESSAGE);
                return;
            }

            // 更新余额
            double newBalance = Double.parseDouble(balanceField.getText());
            card.setBalance(newBalance);

            // 更新密码
            String newPassword = new String(passwordField.getPassword());
            card.setPassword(newPassword);

            // 更新挂失状态
            String isReportLoss = (String) isReportLossCombo.getSelectedItem();
            card.setIsReportLoss(isReportLoss);

            // 更新开户日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date newOpenDate = sdf.parse(openDateField.getText());
            card.setOpenDate(newOpenDate);

            // 保存到数据库
            cardDAO.updateCard(card);

            JOptionPane.showMessageDialog(this, "银行卡信息更新成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
            dispose();

        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "请输入有效的金额格式", "格式错误", JOptionPane.ERROR_MESSAGE);
        } catch (ParseException e) {
            JOptionPane.showMessageDialog(this, "请输入正确的日期格式 (yyyy-MM-dd)", "格式错误", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(this, "更新银行卡信息失败：" + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
        }
    }
}